package com.zkt.oss.utils;

import com.zkt.oss.entity.FileInfo;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/16 22:25
 * @description minio文件操作工具
 **/

@Component
public class MinioUtil {
	
	@Resource
	private MinioClient minioClient;
	
	/**
	 * minioUrl
	 */
	@Value("${minio.url}")
	private String url;
	
	/**
	 * 创建bucket桶
	 *
	 * @param bucketName 桶名称
	 */
	public void createBucket(String bucketName) throws Exception {
		boolean exists = minioClient.bucketExists(
				BucketExistsArgs.builder()
						.bucket(bucketName)
						.build()
		);
		if (!exists) {
			minioClient.makeBucket(
					MakeBucketArgs.builder()
							.bucket(bucketName)
							.build()
			);
		}
	}
	
	/**
	 * 列出所有桶
	 *
	 * @return 所有桶名称
	 */
	public List<String> getAllBuckets() throws Exception {
		List<Bucket> buckets = minioClient.listBuckets();
		return buckets.stream().map(Bucket::name).collect(Collectors.toList());
	}
	
	/**
	 * 删除桶
	 *
	 * @param bucketName 桶名称
	 */
	public void deleteBucket(String bucketName) throws Exception {
		minioClient.removeBucket(
				RemoveBucketArgs.builder()
						.bucket(bucketName)
						.build()
		);
	}
	
	/**
	 * 上传文件
	 *
	 * @param inputStream 文件输入流
	 * @param bucketName  桶名称
	 * @param objectName  文件名
	 */
	public void uploadFile(InputStream inputStream, String bucketName, String objectName) throws Exception {
		minioClient.putObject(
				PutObjectArgs.builder()
						.bucket(bucketName)
						.object(objectName)
						.stream(inputStream, -1, Integer.MAX_VALUE)
						.build()
		);
	}
	
	/**
	 * 下载文件
	 *
	 * @param bucketName 桶名称
	 * @param objectName 文件名
	 * @return 文件输入流
	 */
	public InputStream downloadFile(String bucketName, String objectName) throws Exception {
		return minioClient.getObject(
				GetObjectArgs.builder()
						.bucket(bucketName)
						.object(objectName)
						.build()
		);
	}
	
	/**
	 * 列出当前桶的文件
	 *
	 * @param bucketName 桶名称
	 * @return 文件列表
	 */
	public List<FileInfo> getAllFile(String bucketName) throws Exception {
		Iterable<Result<Item>> results = minioClient.listObjects(
				ListObjectsArgs.builder()
						.bucket(bucketName)
						.build()
		);
		
		List<FileInfo> fileInfoList = new ArrayList<>();
		for (Result<Item> result : results) {
			FileInfo fileInfo = new FileInfo();
			Item item = result.get();
			
			fileInfo.setFileName(item.objectName());
			fileInfo.setDirectoryFlag(item.isDir());
			fileInfo.setEtag(item.etag());
			
			fileInfoList.add(fileInfo);
		}
		
		return fileInfoList;
	}
	
	/**
	 * 删除文件
	 *
	 * @param bucketName 桶名称
	 * @param objectName 文件名
	 */
	public void deleteObject(String bucketName, String objectName) throws Exception {
		minioClient.removeObject(
				RemoveObjectArgs.builder()
						.bucket(bucketName)
						.object(objectName)
						.build()
		);
	}
	
	/**
	 * 获取文件url地址
	 *
	 * @param bucket     桶名称
	 * @param objectName 文件名
	 * @return 文件url地址
	 */
	public String getUrl(String bucket, String objectName) throws Exception {
		return url + "/" + bucket + "/" + objectName;
	}
}